package com.spark.sparkanswerserver.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spark.sparkanswerserver.dao.NodeRepository;
import com.spark.sparkanswerserver.domain.Node;
import com.spark.sparkanswerserver.pojo.DateEnity;
import com.spark.sparkanswerserver.pojo.LinkEnity;
import com.spark.sparkanswerserver.pojo.NodeEntity;
import com.spark.sparkanswerserver.service.NodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author Xing
 * @Date 2023/2/23 13:28
 */
@Service
public class NodeServiceImpl implements NodeService {
    @Autowired
    private NodeRepository repository;

    @Override
    public List<NodeEntity> MatchOne() {
        return null;
    }

    @Override
    public String MatchReason(String name) {
        List<Node> reason = repository.MatchReason(name);
        List<Node> result = repository.MatchResult(name);
        String str1 = "",str = "";
        if (reason.size()==0&&result.size()==0){
            return "抱歉，我的数据库中没有相关病例。";
        }else if (reason.size()!=0||result.size()!=0){
            if (reason.size()!=0){
                str1+="病因有";
                for (Node node : reason) {
                    str1+=node.getName()+"，";
                }
                str1 = str1.substring(0, str1.length()-1)+"等，";
            }
            if (result.size()!=0){
                str1+="可能导致";
                for (Node node : result) {
                    str1+=node.getName()+"，";
                }
                str1 = str1.substring(0, str1.length()-1)+"等。";
                System.out.println(str1);
            }
            str = name+"的"+str1.substring(0,str1.length()-1)+"。";
        }
        return str;
    }

    @Override
    public DateEnity GetNode(String name) {
        List<Node> node = new ArrayList<Node>();
        if (name.equals("")){
            node = repository.MatchAll();
        }else {
            node = repository.FindALL2(name);
        }
        DateEnity date = new DateEnity();
        List<LinkEnity> links = new ArrayList<LinkEnity>();
        List<NodeEntity> nodes = new ArrayList<NodeEntity>();
        node.forEach(person -> {
            /*添加节点*/
            NodeEntity entity = new NodeEntity();
            entity.setId(Math.toIntExact(person.getId()));
            entity.setName(person.getName());
            nodes.add(entity);
            /*添加联系*/
            Integer source = Math.toIntExact(person.getId());
            String sourceName = person.getName();
            if(!CollectionUtils.isEmpty(person.getResult())){
                person.getResult().forEach(p -> {
                    LinkEnity link = new LinkEnity();
                    Integer target = Math.toIntExact(p.getId());
                    String targetName = p.getName();
                    String type = repository.Type(sourceName, targetName);
                    link.setName(type);
                    link.setSource(source);
                    link.setTarget(target);
                    links.add(link);
                });
            }
        });
        if (nodes.size()==0&&repository.MatchNumber(name)>0){
            System.out.println("行:"+name);
            Node allById = repository.findAllById(name);
            NodeEntity entity = new NodeEntity(Math.toIntExact(allById.getId()), allById.getName());
            nodes.add(entity);
        }
        date.setLinks(links);
        date.setNodes(nodes);
        return date;
    }

    @Override
    public boolean DeleteNode(String name) {
        try {
            repository.DeleteResultRelation(name);
        }catch (Exception e) {
            System.out.println(1);
            return false;
        }
        try {
            repository.DeleteReasonRelation(name);
        }catch (Exception e) {
            System.out.println(2);
            return false;
        }
        try {
            repository.deleteByName(name);
        }catch (Exception e) {
            System.out.println(3);
            return false;
        }
        return true;
    }

    @Override
    public Integer MatchNumber(String name) {
        return repository.MatchNumber(name);
    }

    @Override
    public Node CreateNode(String name) {
        return repository.CreateNode(name);
    }

    @Override
    public void CreateRelation(String name1, String name2) {
        repository.CreateRelation(name1,name2);
    }

    @Override
    public Page MatchAllNodes(Map<String,Object> map) {
        Integer pagenum = Integer.parseInt(((String) map.get("pagenum")));
        Integer pagesize = Integer.parseInt(((String) map.get("pagesize")));
        Page page = new Page<>();
        Integer number = (pagenum-1)*pagesize;
        List<Node> nodes = repository.MatchAllNodes(number, pagesize);
        List<NodeEntity> node = new ArrayList<>();
        for (Node node1 : nodes) {
            NodeEntity entity = new NodeEntity();
            entity.setName(node1.getName());
            entity.setId(Math.toIntExact(node1.getId()));
            node.add(entity);
        }
        page.setTotal(MatchNumber());
        page.setCurrent(pagenum);
        page.setSize(pagesize);
        page.setRecords(node);
        return page;
    }

    @Override
    public Integer MatchNumber() {
        return repository.MatchNumber();
    }

}
